package utils

import (
	"net"
	"strings"
)

// GetLocalIP 获取本机的局域网IP地址
func GetLocalIP() string {
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		return T("network.ip.unknown")
	}

	for _, addr := range addrs {
		// 检查地址类型并且不是回环地址
		if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			// 检查是否为IPv4地址
			if ipnet.IP.To4() != nil {
				ip := ipnet.IP.String()
				// 过滤掉一些特殊的IP段
				if strings.HasPrefix(ip, "192.168.") || 
				   strings.HasPrefix(ip, "10.") || 
				   strings.HasPrefix(ip, "172.") {
					return ip
				}
			}
		}
	}

	// 如果没有找到私有IP，尝试获取任何可用的IP
	for _, addr := range addrs {
		if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			if ipnet.IP.To4() != nil {
				return ipnet.IP.String()
			}
		}
	}

	return T("network.ip.unknown")
}

// GetAllLocalIPs 获取所有本机IP地址
func GetAllLocalIPs() []string {
	var ips []string
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		return ips
	}

	for _, addr := range addrs {
		if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			if ipnet.IP.To4() != nil {
				ips = append(ips, ipnet.IP.String())
			}
		}
	}

	return ips
} 